﻿using System.Collections.Generic;
using UnityEngine;

namespace XFramework
{

    public class ResourcesManager
    {
        public static readonly string ModelRootPath = "Model/";
        public static readonly string AudioRootPath = "Audio/";
        public static readonly string UIRootPath = "Prefabs/UI/";
        public static readonly string IconRootPath = "Texture/Icon/";
        public static readonly string TexBgRootPath = "Texture/TexBg/";

        public static List<AssetDataInfo> dicAllAsset = new List<AssetDataInfo>();

        public class AssetDataInfo
        {
            public string assetName;
            public AssetsEnum assetType;
            public Object assetObj;
        }


        //获取所有的图片
        public static Sprite[] GetAllSprite(string assetName, AssetsEnum assetsEnum)
        {
            string path = GetResPath(assetName, assetsEnum);
            Sprite[] allSp = Resources.LoadAll<Sprite>(path);
            return allSp;
        }

        //获取资源路径
        private static string GetResPath(string assetName, AssetsEnum assetsEnum)
        {
            string path = "";
            switch (assetsEnum)
            {
                case AssetsEnum.Model:
                    path = ModelRootPath + assetName;
                    break;
                case AssetsEnum.Audio:
                    path = AudioRootPath + assetName;
                    break;
                case AssetsEnum.UIPrefab:
                    path = UIRootPath + assetName;
                    break;
                case AssetsEnum.Icon:
                    path = IconRootPath + assetName;
                    break;
                case AssetsEnum.TexBg:
                    path = TexBgRootPath + assetName;
                    break;
            }

            return path;
        }

        //销毁对象
        public static void DestroyObj(GameObject obj, bool bImmediate = false)
        {
            if (null != obj)
            {
                if (false == bImmediate)
                {
                    GameObject.Destroy(obj);
                }
                else
                {
                    GameObject.DestroyImmediate(obj);
                }
            }
        }

        //清理某资源
        public static void ClearAsset(string assetName)
        {
            AssetDataInfo fdata = dicAllAsset.Find(x => x.assetName == assetName);
            if (fdata != null)
            {
                dicAllAsset.Remove(fdata);
            }
        }

        //清理并卸载对应类型的资源
        public static void ClearAsset(AssetsEnum etype)
        {
            for (int i = dicAllAsset.Count - 1; i > 0; i--)
            {
                if (dicAllAsset[i].assetType == etype)
                {
                    dicAllAsset.RemoveAt(i);
                }
            }

            Resources.UnloadUnusedAssets();
        }

        //清理并卸载所有资源
        public static void ClearAllAsset()
        {
            dicAllAsset.Clear();
            Resources.UnloadUnusedAssets();
        }

        //获取已加载的资源数量
        public static int GetAssetCount()
        {
            return dicAllAsset.Count;
        }
    }
}